package com.ytzl.itrip.search.com.ytzl.itrip.service.impl;

import com.ytzl.itrip.beans.model.SearchHotCityVO;
import com.ytzl.itrip.beans.model.SearchHotelVO;
import com.ytzl.itrip.search.beans.Hotel;
import com.ytzl.itrip.search.com.ytzl.itrip.service.SearchHotelService;
import com.ytzl.itrip.search.dao.BaseQuery;
import com.ytzl.itrip.utils.common.EmptyUtils;
import com.ytzl.itrip.utils.common.Page;
import com.ytzl.itrip.utils.common.PropertiesUtils;
import org.apache.solr.client.solrj.SolrQuery;

import java.util.List;

/**
 * Created by Admin on 2018/4/27.
 */
public class SearchHotelServiceImpl implements SearchHotelService{
    private  String baseUrl= PropertiesUtils.get("public.properties","baseUrl");

    private BaseQuery<Hotel> hotelBaseQuery=new BaseQuery<>(baseUrl);
    @Override
    public Page<Hotel> searchHotelPage(SearchHotelVO searchHotelVO) throws Exception {
        SolrQuery solrQuery=new SolrQuery("*:*");
        StringBuffer tempQuery=new StringBuffer();
        if (EmptyUtils.isNotEmpty(searchHotelVO.getDestination())){
            tempQuery.append("destination:"+searchHotelVO.getDestination());
        }
        if (EmptyUtils.isNotEmpty(searchHotelVO.getKeywords())){
            tempQuery.append("keyword:" + searchHotelVO.getKeywords());
        }

        solrQuery.setQuery(tempQuery.toString());
        //酒店级别
        if (EmptyUtils.isNotEmpty(searchHotelVO.getHotelLevel())){
            solrQuery.addFilterQuery("hotelLevel:"+searchHotelVO.getHotelLevel());
        }
        //酒店特色id
                 //先将传进来的参数分割成数组,只要有一个数字符合就返回酒店id
                if (EmptyUtils.isNotEmpty(searchHotelVO.getFeatureIds())){
                    String[] split = searchHotelVO.getFeatureIds().split(",");
                    StringBuffer fqQuery=new StringBuffer();
                    for (int i=0;i<split.length;i++){
                        String tureId=split[i];
                        if (i==0){
                            fqQuery.append(" featureIds:*,"+tureId+"*");

                        }else {
                            fqQuery.append(" OR featureIds:*,"+tureId+"*");
                        }
                    }
                    solrQuery.addFilterQuery(fqQuery.toString());
                }


        //最高价
        if (EmptyUtils.isNotEmpty(searchHotelVO.getMaxPrice())){
                    solrQuery.addFilterQuery(" maxPrice:[* TO"+searchHotelVO.getMaxPrice()+"]");
        }
        //最低价
        if (EmptyUtils.isNotEmpty(searchHotelVO.getMinPrice())){
            solrQuery.addFilterQuery(" minPrice:["+searchHotelVO.getMinPrice()+" TO *]");
        }
        //商圈id
        if (EmptyUtils.isNotEmpty(searchHotelVO.getTradeAreaIds())){
            String[] split = searchHotelVO.getTradeAreaIds().split(",");
            StringBuffer fqQuery=new StringBuffer();
            for (int i=0;i<split.length;i++){
                String areaId=split[i];
                if (i==0){
                    fqQuery.append(" iradeAreaIds:*,"+areaId+"*");

                }else {
                    fqQuery.append(" OR iradeAreaIds:*,"+areaId+"*");
                }
            }
            solrQuery.addFilterQuery(fqQuery.toString());
        }

        //字段降序
        if (EmptyUtils.isNotEmpty(searchHotelVO.getDescSort())){
            solrQuery.addSort(searchHotelVO.getDescSort(),SolrQuery.ORDER.desc);
        }
        //字段升序
        if (EmptyUtils.isNotEmpty(searchHotelVO.getAscSort())){
            solrQuery.addSort(searchHotelVO.getAscSort(),SolrQuery.ORDER.desc);
        }
        return hotelBaseQuery.queryPage(solrQuery,searchHotelVO.getPageNo(),searchHotelVO.getPageSize(),searchHotelVO.getPageSize(),Hotel.class);
    }

    @Override
    public List<Hotel> searchHotelByHotCity(SearchHotCityVO searchHotCityVO) throws Exception {
        SolrQuery solrQuery =new SolrQuery("*:*");
        if (EmptyUtils.isNotEmpty(searchHotCityVO.getCityId())){
            solrQuery.addFilterQuery("cityId"+searchHotCityVO.getCityId());
        }
        return hotelBaseQuery.queryList(solrQuery,searchHotCityVO.getCount(),Hotel.class);
    }
}
